﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{

    public partial class FormLogin : Form
    {

        //Database connection:
        static string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename='|datadirectory|\Database1.mdf';Integrated Security=True;User Instance=True";
        static SqlConnection cn = new SqlConnection(connectionString);
        string sCommand;
        //Data adapter that understands SQL databases:
        static SqlDataAdapter da;
        //Table pour stocker les résultats de la requête
        static DataTable dataTable = new DataTable();
        string[,] dataUser = new string[1,4];
        Boolean status = false;

        public FormLogin()
        {
            InitializeComponent();
        }
        

        
        private void FormLogin_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
           try
            {
                //Command to execute           
                sCommand = "SELECT * FROM Utilisateur";
                da = new SqlDataAdapter(sCommand, cn);
                int recordsAffected = da.Fill(dataTable);
          
                //Remplit le tableau de données
                if (recordsAffected > 0)
                {
               
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        
                        string s = dr[1].ToString();
                        if (txtUsername.Text.Equals(dr[1].ToString().Trim()))
                        {
                            string s2 = dr[2].ToString();

                            if (txtPassword.Text.Equals(dr[2].ToString().Trim()))
                            {
                                MessageBox.Show("Bienvenue " + dr[1],"Bienvenue",MessageBoxButtons.OK, MessageBoxIcon.Information);
                               
                                status = true;

                                if (dr[3].ToString().Trim().Equals("admin"))
                                {
                                   
                                    FormAdmin formAdmin = new FormAdmin();
                                    formAdmin.ShowDialog();
                                    this.txtPassword.Text = "";
                                    this.txtUsername.Text = "";
                                    break;
                                }
                                else if (dr[3].ToString().Trim().Equals("employe"))
                                {
                                   
                                    FormEmploye formEmploye = new FormEmploye();
                                    formEmploye.ShowDialog();
                                    this.txtPassword.Text = "";
                                    this.txtUsername.Text = "";
                                    break;
                                }
                            }

                        }

                        status = false;
                    }

                    if (status == false)
                    {
                        MessageBox.Show("Login et/ou mot de passe incorrect(s)","Authentification Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
 
                    }
                    
                }
            }
            catch (SqlException ex)
            {
                string msg = "";
                for (int i = 0; i < ex.Errors.Count; i++)   
                {
                    msg += "Error #" + i + "Message : " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
            }
            finally
            {
               
                    cn.Close();
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
